package models;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.Timer;

public class GameTimer extends Observable
{
    Timer t;
    Game game;
    int seconds;
    int minutes;
    int hours;

    public GameTimer(Game game)
    {
        this.game = game;
        seconds = minutes = hours = 0;

        t = new Timer(1000, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                seconds++;

                if (seconds == 60)
                {
                    seconds = 0;
                    minutes++;
                }

                if (minutes == 60)
                {
                    hours++;
                    minutes = 0;
                }

                setChanged();
                notifyObservers("Timer");
            }
        });
    }

    public void start()
    {
        stop();
        t.start();
    }

    public void stop()
    {
        t.stop();
        minutes = seconds = hours = 0;
        setChanged();
        notifyObservers("Timer");
    }

    public void pause()
    {
        t.stop();
    }

    public void resume()
    {
        t.start();
    }


    //TODO: retornar solo segundos y que la vista tenga la responsavilidad del formato
    public String getTime()
    {
        String result = "";

        if (hours < 10)
            result += "0" + String.valueOf(hours);
        else
            result += String.valueOf(hours);

        result += " : ";

        if (minutes < 10)
            result += "0" + String.valueOf(minutes);
        else
            result += String.valueOf(minutes);

        result += " : ";

        if (seconds < 10)
            result += "0" + String.valueOf(seconds);
        else
            result += String.valueOf(seconds);

        return result;
    }

    public int getTimeInSeconds()
    {
        return seconds + (minutes * 60) + (hours * 3600);
    }
}
